package com.gxuwz.attendance.dao;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.gxuwz.attendance.entity.Teacher;
import com.gxuwz.attendance.vo.Page;
import net.sf.json.JSONArray;

@Repository("TeacherDao")
public class TeacherDao extends SessionFactoryDao {

	/**
	 * 模糊查询教师信息
	 * 
	 * @param teacher
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Teacher> findByPage(Teacher teacher, Page page) {
		List<Teacher> list = new ArrayList<Teacher>();

		String hql = "from Teacher where 1=1 ";
		if(teacher.getCollegeNo() != null && !"".equals(teacher.getCollegeNo())) {
			hql = hql + "and collegeNo = '" + teacher.getCollegeNo() + "' ";
		}
		
		if (teacher.getTeacherName() != null && !"".equals(teacher.getTeacherName())) {
			hql = hql + "and teacherName like '%" + teacher.getTeacherName() + "%' ";

		}

		list = getSession().createQuery(hql).list();

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		// 保存本次查询的总记录数
		session.setAttribute("teacherSize", list.size());
		
		// 分页
		if(page.getPage() * page.getRows() > list.size()) {
			list = list.subList(page.getOffset(), list.size());
		}else {
			list = list.subList(page.getOffset(), page.getPage() * page.getRows());
		}

		return list;
	}

	/**
	 * 添加教师信息
	 * 
	 * @param teacher
	 * @return
	 */
	public boolean addTeacher(Teacher teacher) {
		boolean flag = true;

		Session session = null;

		long timeMillis = System.currentTimeMillis();

		String time = timeMillis + "";

		try {
			session = getSession();
			teacher.setTeacherNo("2019" + time.substring(8));

			// 开启事务
			session.beginTransaction();
			session.save(teacher);
			session.getTransaction().commit();
		} catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}

		return flag;
	}

	/**
	 * 更新教师信息
	 * 
	 * @param student
	 * @return
	 */
	public boolean updateTeacher(Teacher teacher) {
		boolean flag = true;
		Session session = null;
		try {
			session = getSession();
			// 开启事务
			session.beginTransaction();
			session.update(teacher);
			session.getTransaction().commit();
		} catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}

		return flag;
	}

	/**
	 * 批量删除教师信息
	 * 
	 * @param ids
	 * @return
	 */
	public boolean deleteTeacher(String ids) {
		JSONArray array = JSONArray.fromObject(ids);

		Session session = null;
		session = getSession();

		boolean flag = true;

		session.beginTransaction();
		try {

			for (int i = 0; i < array.size(); i++) {
				Teacher teacher = new Teacher();

				teacher.setTeacherNo(array.getJSONObject(i).getString("teacherNo"));

				session.delete(teacher);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			flag = false;
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return flag;
	}


}
